{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "36c8f811-d2d2-4542-a851-a2b471673c55",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of BertForSequenceClassification were not initialized from the model checkpoint at D:\\ideaSpace\\MyPython\\models\\bert-base-chinese and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sentiment-analysis： [{'label': 'LABEL_0', 'score': 0.5135796666145325}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "pipe = pipeline(\n",
    "    task=\"sentiment-analysis\",\n",
    "    model=r\"D:\\ideaSpace\\MyPython\\models\\bert-base-chinese\",\n",
    "    device=\"cpu\"\n",
    ")\n",
    "\n",
    "result = pipe(\"今儿上海可真冷啊\")\n",
    "print(\"sentiment-analysis：\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8b06d51c-2268-4880-bc3d-0ee52e973a13",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cpu\n",
      "Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ner： [{'entity': 'B-address', 'score': np.float32(0.9916643), 'index': 10, 'word': 'new', 'start': 42, 'end': 45}, {'entity': 'I-address', 'score': np.float32(0.9938553), 'index': 11, 'word': 'york', 'start': 46, 'end': 50}, {'entity': 'I-address', 'score': np.float32(0.9911117), 'index': 12, 'word': 'city', 'start': 51, 'end': 55}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "classifier = pipeline(\n",
    "    task=\"ner\",\n",
    "    # model=r\"D:\\ideaSpace\\MyPython\\models\\bert-base-chinese\", # 通用性强，需微调\n",
    "    model=r\"D:\\ideaSpace\\MyPython\\models\\Ernie-3.0-base-chinese-finetuned-ner\", # 中文实体识别优化，精度高\n",
    "    device=\"cpu\"\n",
    ")\n",
    "\n",
    "result = classifier(\"Hugging Face is a French company based in New York City.\")\n",
    "print(\"ner：\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0e0e817-77eb-4e0d-95e1-59d2529e29a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "question-answering： {'score': 0.8099176287651062, 'start': 30, 'end': 54, 'answer': 'huggingface/transformers'}\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "question_answerer = pipeline(\n",
    "    task=\"question-answering\",\n",
    "    model=r\"D:\\ideaSpace\\MyPython\\models\\tinybert-6l-768d-squad2\",\n",
    "    device=\"cpu\"\n",
    ")\n",
    "\n",
    "result = question_answerer(question=\"What is the name of the repository?\", context=\"The name of the repository is huggingface/transformers\")\n",
    "print(\"question-answering：\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "15d1b3a7-b912-4164-ba9c-23bbf15ef436",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cpu\n",
      "Your max_length is set to 142, but your input_length is only 117. Since this is a summarization task, where outputs shorter than the input are typically wanted, you might consider decreasing max_length manually, e.g. summarizer('...', max_length=58)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "summarization： [{'summary_text': ' The Transformer is the first sequence transduction model based entirely on attention . It replaces the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention . For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers .'}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "summarizer = pipeline(task=\"summarization\",\n",
    "                      model=r\"D:\\ideaSpace\\MyPython\\models\\distilbart-cnn-12-6\",\n",
    "                      min_length=8,\n",
    "                      max_length=32)\n",
    "\n",
    "result = summarizer(\"In this work, we presented the Transformer, the first sequence transduction model based entirely on attention, \"\n",
    "           \"replacing the recurrent layers most commonly used in encoder-decoder architectures with multi-headed self-attention. \"\n",
    "           \"For translation tasks, the Transformer can be trained significantly faster than architectures based on recurrent or convolutional layers. \"\n",
    "           \"On both WMT 2014 English-to-German and WMT 2014 English-to-French translation tasks, we achieve a new state of the art. \"\n",
    "           \"In the former task our best model outperforms even all previously reported ensembles.\")\n",
    "\n",
    "print(\"summarization：\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f4d144f5-d1d1-43d7-bb62-02d039cedd4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of WhisperForAudioClassification were not initialized from the model checkpoint at D:\\ideaSpace\\MyPython\\models\\whisper-small and are newly initialized: ['classifier.bias', 'classifier.weight', 'projector.bias', 'projector.weight']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "audio-classification： [{'score': 0.6005539894104004, 'label': 'LABEL_0'}, {'score': 0.3994460701942444, 'label': 'LABEL_1'}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "classifier = pipeline(task=\"audio-classification\", model=r\"D:\\ideaSpace\\MyPython\\models\\whisper-small\")\n",
    "# result = classifier(\"https://hf-mirror.com/datasets/Narsil/asr_dummy/tree/main/mlk.flac\") # 会报ValueError: Malformed soundfile\n",
    "result = classifier(\"D:/ideaSpace/MyPython/datasets/mlk.flac\")\n",
    "print(\"audio-classification：\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6df95ccf-3610-4b4d-93ac-cfd1956e69a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cpu\n",
      "D:\\conda_envs\\rag_learn\\lib\\site-packages\\transformers\\models\\whisper\\generation_whisper.py:573: FutureWarning: The input name `inputs` is deprecated. Please make sure to use `input_features` instead.\n",
      "  warnings.warn(\n",
      "Due to a bug fix in https://github.com/huggingface/transformers/pull/28687 transcription using a multilingual Whisper will default to language detection followed by transcription instead of translation to English.This might be a breaking change for your use case. If you want to instead always translate your audio to English, make sure to pass `language='en'`.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "automatic-speech-recognition： {'text': ' I have a dream, but one day, this nation will rise up, live out the true meaning of its dream.'}\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "transcriber = pipeline(task=\"automatic-speech-recognition\", model=r\"D:\\ideaSpace\\MyPython\\models\\whisper-tiny\")\n",
    "text = transcriber(\"D:/ideaSpace/MyPython/datasets/mlk.flac\")\n",
    "print(\"automatic-speech-recognition：\", text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "86b038d3-0ba0-484a-8c7b-0a4db6b6058f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.\n",
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image-classification： [{'label': 'lynx, catamount', 'score': 0.834252119064331}, {'label': 'wombat', 'score': 0.0051718889735639095}, {'label': 'tiger, Panthera tigris', 'score': 0.005074908956885338}, {'label': 'tabby, tabby cat', 'score': 0.0025985362008213997}, {'label': 'marmot', 'score': 0.0021469832863658667}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "classifier = pipeline(task=\"image-classification\", model=r\"D:\\ideaSpace\\MyPython\\models\\efficientnet-b3\")\n",
    "# 直接使用url会报PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000015F5DBF05E0>\n",
    "# result = classifier(\"https://hf-mirror.com/datasets/huggingface/documentation-images/tree/main/pipeline-cat-chonk.jpeg\")\n",
    "result = classifier(\"D:/ideaSpace/MyPython/datasets/pipeline-cat-chonk.jpeg\")\n",
    "print(\"image-classification：\", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "69ed441d-661f-4581-bba7-177dd0237acf",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cpu\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "object-detection:  [{'score': 0.8501139283180237, 'label': 'bear', 'box': {'xmin': 173, 'ymin': 161, 'xmax': 886, 'ymax': 594}}]\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "detector = pipeline(task=\"object-detection\", model=r\"D:\\ideaSpace\\MyPython\\models\\yolos-tiny\")\n",
    "result = detector(\"D:/ideaSpace/MyPython/datasets/pipeline-cat-chonk.jpeg\")\n",
    "print(\"object-detection: \", result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0892d276-b2c3-4f4c-8122-845377dc3b86",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
